function uploadFileCallback(responseText, statusText) {
    $("#uploadInfo").html('').hide();
    if (statusText = 'success') {
        if (responseText.msgType == 'success') {
            $("#uploadResult").val("success");
            processUploadResult();
        } else {
            $("<p>" + responseText.msg + "</p>").appendTo("#uploadInfo");
            $("#uploadInfo").css('color', 'red');
            processResultBack('failed');
        }
    }
}

function processUploadResult() {
    if ($("#uploadResult").val() == "success") {
        $("#uploadInfo").css('color', 'green');
        processResultBack('success');
    } else {
        $("<p>Upload Failed.</p>").appendTo("#uploadInfo");
        $("#uploadInfo").css('color', 'red');
        processResultBack('failed');
    }
    $("#uploadResult").val(null);
}

function processResultBack(resultType) {
    $('#uploadButton').attr("value", "Upload");
    $('#loadingLogo').css("display", "none");
    $('#uploadButton').removeAttr("disabled");
    $('#uploadButton').parent().removeClass("cancelButtonBox").addClass("orangeButtonBox");
    $('#uploadButton').focus();
    if ('FAILED' == resultType) {
        suryaniHtmlWrapHopupWindow(630, null, '', $('.uploadResultMsgBoxHopup'), true, null, 150);
    } else if ('SUCCESS' == resultType) {
        showUploadMessages('File Successfully Uploaded.', 'blue');
    }
}

function showUploadMessages(messages, fontColor) {
    $("#uploadMessages").html(messages);
    $("#uploadMessages").css('color', fontColor);
    $("#uploadMessages").show();
}

function removeFile(obj) {
    var historyId = $(obj).attr("history-id");
    var params = {"uploadHistoryId": historyId};
    var siblings = $(obj).parents("tr").siblings().size();
    $.ajax({
        type: "POST",
        url: golbalRootUrl + "upload/history/removeFile",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(params),
        success: function (result) {
            $(obj).parents("tr").remove();
            if (0 == siblings) {
                $("#uploadResultDiv").hide();
            } else {
                $("#uploadHistoryBody tr:odd").addClass("pinkBg");
                $("#uploadHistoryBody tr:even").removeClass("pinkBg");
            }
        }
    });
}

function appendUploadHistory(obj) {
    var history = obj;
    if (null != history.fileIdentity && "" != history.fileIdentity) {
        $("tr[file-uuid='" + history.fileIdentity + "']").remove();
    }
    var dateTime = new Date(Date.parse(obj.localUploadTime));
    var submissionDate = format(dateTime, 'MM/dd/yy hh:mm aa');
    $("<tr id='tr' file-uuid='" + history.fileIdentity + "'><td style='width:250px;text-align: left;'>" + history.originalFileName + "</td><td style='width:150px;text-align: left;'>" + history.submittedBy
        + "<td style='width:150px;text-align: right;'>" + submissionDate + "</td><td style='text-align: left;'>" + history.resultInfo +
        "</td><td style='width:100px;'><a onclick='viewFile(this)' history-id='" + history.id + "' href='javascript:void(0)'>View</a></td><td style='width:100px;'></td></tr>").appendTo("#uploadHistoryBody");
    $("#uploadHistoryBody tr:odd").addClass("pinkBg");
    $("#uploadHistoryBody tr:even").removeClass("pinkBg");
    $("#uploadResultDiv").show();
}

function getTimeByTimeStr(dateString) {
    var timeArr = dateString.split(" ");
    var d = timeArr[0].split("-");
    var t = timeArr[1].split(":");
    var realDate = new Date(d[0], (d[1] - 1), d[2], t[0], t[1], 0, 0);
    return realDate;
}

function viewFile(obj) {
    window.open(golbalRootUrl + "upload/history/viewFile" + "?historyId=" + $(obj).attr('history-id'));
}

function checkForFileApiSupport() {
    if (window.File && window.FileReader && window.FileList && window.Blob) {
        return true;
    } else {
        return false;
    }
}
